package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

// 购物车
@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    // 添加购物车
    @Override
    public Cart add(Cart cartParam) {
        // 1.先查询购物车是否有数据
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        // 用户id
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        // 套餐id 动态sql
        qw.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
        // 菜品id 动态sql
        qw.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());
        Cart cart = cartMapper.selectOne(qw);

        // 2.判断
        if (cart==null) { // 如果为空，新增一条记录
            cartParam.setUserId(UserHolder.get().getId()); // 补全用户id
            cartParam.setNumber(1); // 补全数量
            cartParam.setCreateTime(new Date()); // 手动填充时间
            cartMapper.insert(cartParam);
            cart = cartParam;
        }else{ // 存在记录，购买数量+！
            cart.setNumber(cart.getNumber()+1);
            cartMapper.updateById(cart);
        }
        // 返回购物车
       return cart ;
    }

    // 查看购物车
    @Override
    public List<Cart> findList() {
        // 1.构建条件查询  select * from shopping_cart where user_id = ?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId, UserHolder.get().getId());// 从线程中获取登录用户id
        // 2.执行查询并返回
        return cartMapper.selectList(qw);
    }


    //清空购物车
    @Override
    public void clean() {
        //构建条件
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId,UserHolder.get().getId());
        //执行删除
        cartMapper.delete(qw);
    }

    //修改购物车
    @Override
    public Cart update(Cart cartParam) {
        //1.确定更新的购物车
        Long dishId = cartParam.getDishId();
        Long setmealId  = cartParam.getSetmealId();

        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        // 用户id
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        // 套餐id 动态sql
        qw.eq(dishId != null, Cart::getDishId,dishId);
        // 菜品id 动态sql
        qw.eq(setmealId != null, Cart::getSetmealId, cartParam.getSetmealId());
        Cart cart = cartMapper.selectOne(qw);

        // 2.执行更新
        if (cart != null) {
            cart.setNumber(cart.getNumber()-1);
                if (cart.getNumber()<=0){
                    //删除
                    cartMapper.deleteById(cart.getId());
                }else{
                    //更新
                    cartMapper.updateById(cart);
                }
        }
        return cart ;
    }

}
